package com.qqclient.service;

import com.qqcommon.Message;
import com.qqcommon.MessageType;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class FileClientService {
    public void sendFileToOne(String src,String dest,String sender,String getterID) throws IOException {
        Message message = new Message();
        message.setSender(sender);
        message.setMesType(MessageType.MESSAGE_File_MES);
        message.setDest(dest);
        message.setSrc(src);
        message.setGetter(getterID);
        //将文件读出
        FileInputStream fileInputStream = null;
        byte[] bytes = new byte[(int) new File(src).length()];
        //
        FileInputStream fileInputStream1 = new FileInputStream(src);
        fileInputStream1.read(bytes);//将文件读入字节数组中
        message.setFileLBytes(bytes);

        if (fileInputStream != null) {
            fileInputStream.close();
        }
        System.out.println("\n"+message.getSender()+"给"+message.getGetter()+"发送文件: "+src+"到"+message.getGetter()+"的电脑目录"+dest);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClinentConnectServerThrend.getClinentConnectServerThrend(sender).getSocket().getOutputStream());
        objectOutputStream.writeObject(message);

    }
}
